####
# Published in Research & Politics  ----

### Article: What explains election-driven family conflicts?
### Authors: Malu A. C. Gatto, João Gabriel Leal, and Débora Thomé

# .  ----
##
# Datas and Packages ----

pacman::p_load(sjPlot, tidyverse, patchwork, ggthemes, ggthemes, gt, performance)

load("artigos/familia_voto/arquivos_research_politics/data_base_1.Rda")
load("artigos/familia_voto/arquivos_research_politics/data_base_2.Rda")

# . ----
####
##
# Main Text ----

# . ----
####
##
## Figure 1 ----

# Panel A

a <- data_base_1 %>% 
  filter(is.na(namorado_esposo_filtro)) |>
  group_by(q4_15_familiatoda) |> 
  summarise(quantidade = n()) |> 
  ungroup() |> 
  mutate(soma = sum(quantidade),
         perc = round (quantidade/soma * 100,1)) |> 
  ggplot()+
  aes(x = q4_15_familiatoda, y = perc) +
  geom_col(width = 0.6, position = "dodge", alpha= .5)+
  geom_text(aes(
    label = perc  
  ), size = 3.2, position = position_dodge(width = 1), hjust = 0.4, vjust = 0)+
  scale_y_continuous(labels = function(x) paste0(x, "%"), limits = c(0, 100))+
  #scale_x_reverse() +  
  #coord_flip()+
  theme_fivethirtyeight()+
  labs(
    title = "Panel A",
    subtitle = "Share of respondents who reported family conflicts", 
    x = "Number of conflicts",
    y = "",
    fill = NULL,
    caption = ""
  )+
  theme(legend.title = element_text(family = "Times New Roman"), 
        legend.position = "bottom",
        title = element_text(size = 11, family = "Times New Roman"),
        axis.title.y = element_text(size = 11, family = "Times New Roman"),
        axis.text.y = element_text(size = 11, family = "Times New Roman"),
        axis.text.x = element_text(size = 11, family = "Times New Roman"),
        axis.title.x = element_text(size = 11,family = "Times New Roman"),
        legend.text = element_text(size = 11, family = "Times New Roman"),
        plot.caption = element_text(hjust = 0, size = 11, family = "Times New Roman" ),
        panel.background = element_rect(fill = "white", colour = "white", color = "white"),
        plot.background = element_rect(fill = "white", colour = "white", color = "white"),
        legend.background = element_rect(fill = "white"),
        panel.grid.major.x = element_line(colour = "#eceff2", size = 0.7),
        panel.grid.major.y = element_line(colour = "#eceff2", size = 0.7),
        legend.key = element_rect(fill = "white", color = "white"),
        strip.background = element_rect(
          fill = "white", colour = "white"
        ))

# Panel B

b <- 
  data_base_1 |>
  filter(is.na(namorado_esposo_filtro)) |> 
  group_by(electoral_divergences_22) |> 
  summarise(quantidade = n()) |> 
  ungroup() |> 
  mutate(soma = sum(quantidade),
         perc = round (quantidade/soma * 100,1)) |> 
  ggplot()+
  aes(x = as.character(electoral_divergences_22), y = perc) +
  geom_col(width = 0.8, position = "dodge", alpha= .5)+
  geom_text(aes(
    label = perc  
  ), size = 3.2, position = position_dodge(width = 1), hjust = 0.4, vjust = 0)+
  
  scale_y_continuous(labels = function(x) paste0(x, "%"), limits = c(0, 100))+
  #scale_x_reverse() +  
  #coord_flip()+
  theme_fivethirtyeight()+
  labs(
    title = "Panel B",
    subtitle = "Share of respondents who reported electoral divergences", 
    x = "Number of divergences",
    y = "",
    fill = NULL,
    caption = ""
  )+
  theme(legend.title = element_text(family = "Times New Roman"), 
        legend.position = "bottom",
        title = element_text(size = 11, family = "Times New Roman"),
        axis.title.y = element_blank(),
        axis.text.y = element_blank(),
        axis.text.x = element_text(size = 11, family = "Times New Roman"),
        axis.title.x = element_text(size = 11,family = "Times New Roman"),
        legend.text = element_text(size = 11, family = "Times New Roman"),
        plot.caption = element_text(hjust = 0, size = 11, family = "Times New Roman" ),
        panel.background = element_rect(fill = "white", colour = "white", color = "white"),
        plot.background = element_rect(fill = "white", colour = "white", color = "white"),
        legend.background = element_rect(fill = "white"),
        panel.grid.major.x = element_line(colour = "#eceff2", size = 0.7),
        panel.grid.major.y = element_line(colour = "#eceff2", size = 0.7),
        legend.key = element_rect(fill = "white", color = "white"),
        strip.background = element_rect(
          fill = "white", colour = "white"
        ))

a + b

# Save

# ggsave("artigos/familia_voto/resultados_familia_voto/figura_2.tiff",
#        width = 25, height = 12, units = "cm")

rm(a,b)

# . ----
####
##
## Figure 2 ----

####
##
### Main Models (Rare event) ----

library(logistf)

#### Model 1: Number of Conflicts (rare event) ----

m_1 <- logistf(
  q4_15_familiatoda  ~ 
    
    `Electoral divergences (ED)` +    
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income,
  firth = FALSE,
  pl = FALSE,
  data = data_base_2)

##
#### Model 2: Number of Conflicts (rare event) ----
# Obs: INTERACTION: Electoral divergences*Antagonism - PT

m_2 <- logistf(
  q4_15_familiatoda  ~ #
    
    `Electoral divergences (ED)` +     
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income +
    `Electoral divergences (ED)` * `Antagonism Lula supporters (ALS)`,
  firth = FALSE,
  pl = FALSE,
  data = data_base_2
)

##
#### Model 3: Number of Conflicts (rare event) ----
# Obs: INTERACTION: Electoral divergences*Antagonism - Bolsonaro

m_3 <- logistf(
  q4_15_familiatoda  ~ 
    
    `Electoral divergences (ED)` +     
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income+
    `Electoral divergences (ED)` * `Antagonism Bolsonaro supporters (ABS)` ,
  firth = FALSE,
  pl = FALSE,
  data =data_base_2
)

##
#### Model 4: Number of Conflicts (rare event) ----
# Obs: INTERACTION: Electoral divergences*Non-heterosexual

m_4 <- logistf(
  q4_15_familiatoda  ~ 
    
    `Electoral divergences (ED)` +   
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income+
    `Electoral divergences (ED)` * `Non-heterosexual`,
  firth = FALSE,
  pl = FALSE,
  data = data_base_2
)

#

plot_models(m_4, m_3, m_2, m_1,
            rm.terms = c("Age", "Education", "Income", "Family_size"), # Para não plotar no grafico
            show.values = TRUE, # modelo grafico 
            #value.offset = .3,
            vline.color = "black",
            line.size = 1,
            m.labels = c("Model 4", "Model 3", "Model 2", "Model 1"),
            ci.lvl = 0.95,
            grid = TRUE,
            show.legend = FALSE)+
  scale_x_discrete(labels=
                     list(
                       `Electoral divergences (ED):Non-heterosexual` = "ED X Non-heterosexual",
                       `Electoral divergences (ED):Antagonism Bolsonaro supporters (ABS)` = "ED X ABS",
                       `Electoral divergences (ED):Antagonism Lula supporters (ALS)` = "ED X ALS"
                     ))+
  labs(
    #title = "Family conflicts in the 2022 elections",
    y = "Odds Ratios",
    col = "")+
  scale_y_continuous(limits = c(0, 4))+
  scale_color_manual(values = c("#333133", "#B1B1B1","#333133","#B1B1B1"))+
  theme_fivethirtyeight()+
  theme(legend.title = element_text(family = "Times New Roman"), 
        legend.position = "bottom",
        title = element_text(size = 11, family = "Times New Roman"),
        axis.title.y = element_text(size = 11, family = "Times New Roman"),
        axis.text.y = element_text(size = 11, family = "Times New Roman"),
        axis.text.x = element_text(size = 11, family = "Times New Roman"),
        axis.title.x = element_text(size = 11,family = "Times New Roman"),
        legend.text = element_text(size = 11, family = "Times New Roman"),
        plot.caption = element_text(hjust = 0, size = 11, family = "Times New Roman" ),
        panel.background = element_rect(fill = "white", colour = "white", color = "white"),
        plot.background = element_rect(fill = "white", colour = "white", color = "white"),
        legend.background = element_rect(fill = "white"),
        panel.grid.major.x = element_line(colour = "#eceff2", size = 0.7),
        panel.grid.major.y = element_line(colour = "#eceff2", size = 0.7),
        legend.key = element_rect(fill = "white", color = "white"),
        strip.background = element_rect(fill = "white", colour = "white"))

#ggsave("artigos/familia_voto/resultados_familia_voto/m_dummy_grid.tiff", width = 26, height = 12, units = "cm")

rm(m_1, m_2, m_3, m_4)

# . ----
####
##
# Analyses presented in the Appendix  ----

# . ----
####
##
## Appendix A ----

pt_bolso_bd <- data_base_1 |> 
  select(response_id, q4_19_1, q4_19_2, q4_13) |>
  mutate(`PT voters` = ifelse(q4_13 == "Lula (PT)" ,"PT voters", NA),  # To operationalize: Those who voted for the Workers' Party (PT) in the second round of the 2022 election.
         `Bolsonaro voters` = ifelse(q4_13 == "Jair Bolsonaro (PL)" , "Bolsonaro voters", NA)) # To operationalize: Those who voted for Jair Bolsonaro in the second round of the 2022 election.

# How do PT voters feel about those who voted for Lula (q4_19_2)

petista_pt <- pt_bolso_bd |>
  filter(`PT voters` == "PT voters") |> 
  select(`PT voters`, q4_19_2) |>
  mutate(`PT voters` = NULL) |>
  group_by(q4_19_2) |> 
  summarise(valor = n()) |>  
  mutate(escala= as.character(q4_19_2), 
         soma = sum(valor), 
         perc = round((valor/soma*100),1), 
         q4_19_2 = NULL,
         valor = NULL,
         soma = NULL,
         identifi = "Voted for Lula") 

# How do PT voters feel about those who voted for Bolsonaro (q4_19_1)?

petista_bolso <-  pt_bolso_bd |>
  filter(`PT voters` == "PT voters") |> 
  select(`PT voters`, q4_19_1) |>
  mutate(`PT voters` = NULL) |>
  group_by(q4_19_1) |> 
  summarise(valor = n()) |>  
  mutate(escala= as.character(q4_19_1), 
         soma = sum(valor), 
         perc = round((valor/soma*100),1), 
         q4_19_1 = NULL,
         valor = NULL,
         soma = NULL,
         identifi = "Voted for Bolsonaro") 

# Combine: Identification of PT voters.

ident_petistas <- rbind(petista_pt, petista_bolso) |> 
  mutate(tipo_afeto = "PT voters") 

# How do Bolsonaro voters feel about those who voted for Lula (q4_19_2)

bolsonarista_pt <- pt_bolso_bd |>
  filter(`Bolsonaro voters` == "Bolsonaro voters") |> 
  select(`Bolsonaro voters`, q4_19_2) |>
  mutate(`Bolsonaro voters` = NULL) |>
  group_by(q4_19_2) |> 
  summarise(valor = n()) |>  
  mutate(escala= as.character(q4_19_2), 
         soma = sum(valor), 
         perc = round((valor/soma*100),1), 
         q4_19_2 = NULL,
         valor = NULL,
         soma = NULL,
         identifi = "Voted for Lula") 

# How do Bolsonaro supporters (6–10 on the scale) feel about those who voted for Bolsonaro (q4_19_1)

bolsonarista_bolso <- pt_bolso_bd |>
  filter(`Bolsonaro voters` == "Bolsonaro voters") |> 
  select(`Bolsonaro voters`, q4_19_1) |>
  mutate(`Bolsonaro voters` = NULL) |>
  group_by(q4_19_1) |> 
  summarise(valor = n()) |>  
  mutate(escala= as.character(q4_19_1), 
         soma = sum(valor), 
         perc = round((valor/soma*100),1), 
         q4_19_1 = NULL,
         valor = NULL,
         soma = NULL,
         identifi = "Voted for Bolsonaro") 

ident_bolsonarista <- rbind(bolsonarista_pt, bolsonarista_bolso) |> 
  mutate(tipo_afeto = "Bolsonaro voters") 

# Produce figure

rbind(ident_petistas, ident_bolsonarista)|> 
  mutate (categoria_escala = as.character(escala),
          escala = as.numeric (escala)) |> 
  ggplot() +
  aes(x = fct_reorder(categoria_escala, escala), y = perc, fill = tipo_afeto) +
  geom_col(position = "dodge", alpha= .5)+
  facet_grid(cols = vars(identifi)) +
  geom_text(aes(
    #label = paste0(round(perc, 1),"%")
    label = perc  
  ), size = 3.2, position = position_dodge(width = 1), hjust = -0.2,vjust = 0.4)+
  scale_y_continuous(labels = function(x) paste0(x, "%"))+
  scale_fill_manual(values = c("#000000", "#A9A9A9"))+
  coord_flip()+
  theme_fivethirtyeight()+
  labs(
    subtitle = "How strongly do you identify with people who:", 
    x = "Not at all                                                                                A lot",
    y = NULL,
    fill = NULL,
    caption = " 
       "
  )+
  theme(legend.title = element_text(family = "Times New Roman"), 
        legend.position = "bottom",
        title = element_text(size = 11, family = "Times New Roman"),
        axis.title.y = element_text(size = 11, family = "Times New Roman"),
        axis.text.y = element_text(size = 11, family = "Times New Roman"),
        axis.text.x = element_text(size = 11, family = "Times New Roman"),
        axis.title.x = element_text(size = 11,family = "Times New Roman"),
        legend.text = element_text(size = 11, family = "Times New Roman"),
        plot.caption = element_text(hjust = 0, size = 11, family = "Times New Roman" ),
        panel.background = element_rect(fill = "white", colour = "white", color = "white"),
        plot.background = element_rect(fill = "white", colour = "white", color = "white"),
        legend.background = element_rect(fill = "white"),
        panel.grid.major.x = element_line(colour = "#eceff2", size = 0.7),
        panel.grid.major.y = element_line(colour = "#eceff2", size = 0.7),
        legend.key = element_rect(fill = "white", color = "white"),
        strip.background = element_rect(
          fill = "white", colour = "white"
        )
  )

# Save

# ggsave("artigos/familia_voto/resultados_familia_voto/apendex/figura_1.tiff",
#        width = 34, height = 17, units = "cm")

rm (pt_bolso_bd,
    petista_pt ,
    petista_bolso,
    ident_petistas,
    bolsonarista_pt,
    bolsonarista_bolso,
    ident_bolsonarista)

# . ----
####
##
## Appendix C  ----

# . ----
###
##
### Appendix C. 1 ----

a <- data_base_1 |> 
  filter(is.na(namorado_esposo_filtro)) 

#### Family Conflicts ----

b_fc <- as.data.frame(t(unclass(summary(a$q4_15_familiatoda)))) |> 
  mutate(`Frequency (%)` = "",
         Variable = "Family Conflicts")

#### `Electoral divergences (ED)`   ----

b_ed <- as.data.frame(t(unclass(summary(a$electoral_divergences_22)))) |> 
  mutate(`Frequency (%)` = "",
         Variable = "Electoral divergences (ED)")

#### `Antagonism Lula supporters (ALS)` ----

b_als <- as.data.frame(t(unclass(summary(a$q4_19_2_e)))) |> 
  mutate(`Frequency (%)` = "",
         Variable = "Antagonism Lula supporters (ALS)")

#### `Antagonism Bolsonaro supporters (ABS)`    ----

b_abs <- as.data.frame(t(unclass(summary(a$q4_19_1_e)))) |> 
  mutate(`Frequency (%)` = "",
         Variable = "Antagonism Bolsonaro supporters (ABS)")

#### `Political salience`  ----

b_ps <- as.data.frame(t(unclass(summary(a$conversa_politica)))) |> 
  mutate(`Frequency (%)` = "",
         Variable = "Political salience")

#### `Women`   ----

b_w <- as.data.frame(t(unclass(summary(a$mulher)))) |> 
  mutate(`Frequency (%)` = "",
         Variable = "Women")

#### `Non-white` ----

b_nw <- as.data.frame(t(unclass(summary(a$nao_branco)))) |> 
  mutate(`Frequency (%)` = "",
         Variable = "Non-white")

#### `Non-heterosexual` ----

b_nh <- as.data.frame(t(unclass(summary(a$nao_heterosexual)))) |> 
  mutate(`Frequency (%)` = "",
         Variable = "Non-heterosexual")

#### `Evangelical`  ----

b_e <- as.data.frame(t(unclass(summary(a$evangelical)))) |> 
  mutate(`Frequency (%)` = "",
         Variable = "Evangelical")

#### Family_size ----

b_fs <- as.data.frame(t(unclass(summary(a$q21_familiasoma)))) |> 
  mutate(`Frequency (%)` = "",
         Variable = "Family Size")

#### Age ----

b_a <- as.data.frame(t(unclass(summary(a$age)))) |> 
  mutate(`Frequency (%)` = "",
         Variable = "Age")

#### Education  ----

b_edu <- as.data.frame(t(unclass(summary(a$escolaridade)))) |> 
  mutate(`Frequency (%)` = "",
         Variable = "Education")

#### Income----

b_i <- as.data.frame(t(unclass(summary(a$renda)))) |> 
  mutate(`Frequency (%)` = "",
         Variable = "Income")

#### All data ----

bd_juntos <-
  rbind (
    # "Family Conflicts" 
    b_fc, 
    ## `Electoral divergences (ED)`  
    b_ed,
    ## `Antagonism Lula supporters (ALS)` 
    b_als,
    ## `Antagonism Bolsonaro supporters (ABS)`    
    b_abs,
    ## `Political salience`  
    b_ps,
    ## `Women`   
    b_w,
    ## `Non-white` 
    b_nw,
    ## `Non-heterosexual` 
    b_nh, 
    ## `Evangelical`  
    b_e, 
    ## Family_size 
    b_fs, 
    ## Age 
    b_a, 
    ## Education  
    b_edu, 
    ## Income
    b_i
  ) |> 
  mutate(Mean = round(Mean,2)) |> 
  select(
    Variable,
    Min.,
    `1st Qu.`,
    Median,
    Mean,
    `3rd Qu.`,
    Max.
  ) 

## Delete dataframes that will no longer be used in analyses
##

rm(
  b_fc, 
  ## `Electoral divergences (ED)`  
  b_ed,
  ## `Antagonism Lula supporters (ALS)` 
  b_als,
  ## `Antagonism Bolsonaro supporters (ABS)`    
  b_abs,
  ## `Political salience`  
  b_ps,
  ## `Women`   
  b_w,
  ## `Non-white` 
  b_nw,
  ## `Non-heterosexual` 
  b_nh, 
  ## `Evangelical`  
  b_e, 
  ## Family_size 
  b_fs, 
  ## Age 
  b_a, 
  ## Education  
  b_edu, 
  ## Income
  b_i)

###
###

## Family Conflicts - Standard Deviation

sd_b_fc <- a |> 
  summarise("Standard Deviation" = round(sd(q4_15_familiatoda),2)) |> 
  mutate(Variable = "Family Conflicts")

## `Electoral divergences (ED)` - Standard Deviation  

sd_b_ed <- a |> 
  summarise("Standard Deviation" = round(sd(electoral_divergences_22),2)) |> 
  mutate(Variable = "Electoral divergences (ED)")

## `Antagonism Lula supporters (ALS)` - Standard Deviation 

sd_b_als <- a |>
  summarise("Standard Deviation" = round(sd(q4_19_2_e),2)) |> 
  mutate(Variable = "Antagonism Lula supporters (ALS)")

## `Antagonism Bolsonaro supporters (ABS)` - Standard Deviation    

sd_b_abs <- a |>
  summarise("Standard Deviation" = round(sd(q4_19_1_e),2)) |> 
  mutate(Variable = "Antagonism Bolsonaro supporters (ABS)")

## `Political salience` - Standard Deviation 

sd_b_ps <- a |>
  summarise("Standard Deviation" = round(sd(conversa_politica),2)) |>  
  mutate(Variable = "Political salience")

## `Women` - Standard Deviation   

sd_b_w <- a |>
  summarise("Standard Deviation" = round(sd(mulher),2)) |> 
  mutate(Variable = "Women")

## `Non-white` - Standard Deviation 

sd_b_nw <- a |>
  summarise("Standard Deviation" = round(sd(nao_branco),2)) |> 
  mutate(Variable = "Non-white")

## `Non-heterosexual` - Standard Deviation 

sd_b_nh <- a |>
  summarise("Standard Deviation" = round(sd(nao_heterosexual),2)) |> 
  mutate(Variable = "Non-heterosexual")

## `Evangelical` - Standard Deviation  

sd_b_e <- a |>
  summarise("Standard Deviation" = round(sd(evangelical),2)) |> 
  mutate(Variable = "Evangelical")

## Family_size - Standard Deviation 

sd_b_fs <- a |>
  summarise("Standard Deviation" = round(sd(q21_familiasoma),2)) |> 
  mutate(Variable = "Family Size")

## Age - Standard Deviation 

sd_b_a <- a |>
  summarise("Standard Deviation" = round(sd(age),2)) |> 
  mutate(Variable = "Age")

## Education - Standard Deviation  

sd_b_edu <- a |>
  summarise("Standard Deviation" = round(sd(escolaridade),2)) |> 
  mutate(Variable = "Education")

## Income - Standard Deviation

sd_b_i <- a |>
  summarise("Standard Deviation" = round(sd(renda),2)) |> 
  mutate(Variable = "Income")

## All data - Standard Deviation 

sd_bd_juntos <-
  rbind (
    # "Family Conflicts" 
    sd_b_fc, 
    ## `Electoral divergences (ED)`  
    sd_b_ed,
    ## `Antagonism Lula supporters (ALS)` 
    sd_b_als,
    ## `Antagonism Bolsonaro supporters (ABS)`    
    sd_b_abs,
    ## `Political salience`  
    sd_b_ps,
    ## `Women`   
    sd_b_w,
    ## `Non-white` 
    sd_b_nw,
    ## `Non-heterosexual` 
    sd_b_nh, 
    ## `Evangelical`  
    sd_b_e, 
    ## Family_size 
    sd_b_fs, 
    ## Age 
    sd_b_a, 
    ## Education  
    sd_b_edu, 
    ## Income
    sd_b_i
  ) 

## Delete dataframes that will no longer be used in analyses
##

rm (
  # "Family Conflicts" 
  sd_b_fc, 
  ## `Electoral divergences (ED)`  
  sd_b_ed,
  ## `Antagonism Lula supporters (ALS)` 
  sd_b_als,
  ## `Antagonism Bolsonaro supporters (ABS)`    
  sd_b_abs,
  ## `Political salience`  
  sd_b_ps,
  ## `Women`   
  sd_b_w,
  ## `Non-white` 
  sd_b_nw,
  ## `Non-heterosexual` 
  sd_b_nh, 
  ## `Evangelical`  
  sd_b_e, 
  ## Family_size 
  sd_b_fs, 
  ## Age 
  sd_b_a, 
  ## Education  
  sd_b_edu, 
  ## Income
  sd_b_i) 


## Merge data
##

bd_juntos_completo <- left_join(bd_juntos, sd_bd_juntos,
                                by = join_by(Variable))

bd_juntos_completo <- bd_juntos_completo |> 
  select(Variable,
         Min.,
         Max.,
         Mean,
         `Standard Deviation`) |> 
  gt()

rm(bd_juntos, sd_bd_juntos, a,
   bd_juntos_completo)

# gtsave(bd_juntos_completo, "tabela_descritiva.html")
# gtsave(bd_juntos_completo, "tabela_descritiva.rtf")

# . ----
####
##
### Appendix C. 2  ----

# Number of cases, by type of family member

# q21_namorado_esposo

bd_q21_namorado_esposo <- 
  data_base_1 |> 
  filter(is.na(namorado_esposo_filtro)) |> 
  group_by(q21_namorado_esposo) |> 
  summarise(N = n ()) |>
  ungroup() |> 
  rename (Description =  q21_namorado_esposo) |> 
  mutate(soma = sum(N),
         Percentage = N / soma * 100,
         #soma = NULL
  ) |> 
  filter(Description == 1) |> 
  mutate(Description = ifelse(Description == 1, "Spouse/Partner", 0))


# q21_avomaterna # Maternal Grandmother

bd_q21_avomaterna <- 
  data_base_1 |> 
  filter(is.na(namorado_esposo_filtro)) |> 
  group_by(q21_avomaterna) |> 
  summarise(N = n ()) |>
  ungroup() |> 
  rename (Description =  q21_avomaterna) |> 
  mutate(soma = sum(N),
         Percentage = N / soma * 100,
         #soma = NULL
  ) |> 
  filter(Description == 1) |> 
  mutate(Description = ifelse(Description == 1, "Maternal Grandmother", 0))

# q21_avopaterna # Paternal Grandmother

bd_q21_avopaterna <- 
  data_base_1|> 
  filter(is.na(namorado_esposo_filtro)) |> 
  group_by(q21_avopaterna) |> 
  summarise(N = n ()) |>
  ungroup() |> 
  rename (Description = q21_avopaterna) |> 
  mutate(soma = sum(N),
         Percentage = N / soma * 100,
         #soma = NULL
  ) |> 
  filter(Description == 1) |> 
  mutate(Description = ifelse(Description == 1, "Paternal Grandmother", 0))

# q21_avomaterno # Maternal Grandfather            

bd_q21_avomaterno <-
  data_base_1 |> 
  filter(is.na(namorado_esposo_filtro)) |> 
  group_by(q21_avomaterno) |> 
  summarise(N = n ()) |>
  ungroup() |> 
  rename (Description = q21_avomaterno) |> 
  mutate(soma = sum(N),
         Percentage = N / soma * 100,
         #soma = NULL
  ) |> 
  filter(Description == 1) |> 
  mutate(Description = ifelse(Description == 1, "Maternal Grandfather", 0))

# q21_avopaterno # Paternal Grandfather           

bd_q21_avopaterno <-
  data_base_1 |> 
  filter(is.na(namorado_esposo_filtro)) |> 
  group_by(q21_avopaterno) |> 
  summarise(N = n ()) |>
  ungroup() |> 
  rename (Description = q21_avopaterno) |> 
  mutate(soma = sum(N),
         Percentage = N / soma * 100,
         #soma = NULL
  ) |> 
  filter(Description == 1) |> 
  mutate(Description = ifelse(Description == 1, "Paternal Grandfather", 0))

# q21_esposo # Husband

# bd_q21_esposo <-
# banco_pos_eleicao_wide |> 
#   group_by(q21_esposo) |> 
#   summarise(N = n ()) |>
#   ungroup() |> 
#   rename (Description = q21_esposo) |> 
#   mutate(soma = sum(N),
#          Percentage = N / soma * 100,
#          soma = NULL,) |> 
#   filter(Description == 1) |> 
#   mutate(Description = ifelse(Description == 1, "Husband", 0))

# q21_filha # Daughter             

bd_q21_filha <-
  data_base_1 |> 
  filter(is.na(namorado_esposo_filtro)) |> 
  group_by(q21_filha) |> 
  summarise(N = n ()) |>
  ungroup() |> 
  rename (Description = q21_filha) |> 
  mutate(soma = sum(N),
         Percentage = N / soma * 100,
         #soma = NULL
  ) |> 
  filter(Description == 1) |> 
  mutate(Description = ifelse(Description == 1, "Daughter(s)", 0))

# q21_filho # Son              

bd_q21_filho <-
  data_base_1 |> 
  filter(is.na(namorado_esposo_filtro)) |> 
  group_by(q21_filho) |> 
  summarise(N = n ()) |>
  ungroup() |> 
  rename (Description = q21_filho) |> 
  mutate(soma = sum(N),
         Percentage = N / soma * 100,
         #soma = NULL
  ) |> 
  filter(Description == 1) |> 
  mutate(Description = ifelse(Description == 1, "Son(s)", 0))

# q21_mae # Mother                   

bd_q21_mae <-
  data_base_1 |> 
  filter(is.na(namorado_esposo_filtro)) |> 
  group_by(q21_mae) |> 
  summarise(N = n ()) |>
  ungroup() |> 
  rename (Description = q21_mae) |> 
  mutate(soma = sum(N),
         Percentage = N / soma * 100,
         #soma = NULL
  ) |> 
  filter(Description == 1) |> 
  mutate(Description = ifelse(Description == 1, "Mother", 0))

# q21_namorado # Boyfriend/Girlfriend              

# bd_q21_namorado <-
# banco_pos_eleicao_wide |> 
#   group_by(q21_namorado) |> 
#   summarise(N = n ()) |>
#   ungroup() |> 
#   rename (Description = q21_namorado) |> 
#   mutate(soma = sum(N),
#          Percentage = N / soma * 100,
#          soma = NULL,) |> 
#   filter(Description == 1) |> 
#   mutate(Description = ifelse(Description == 1, "Boyfriend", 0))

# q21_pai # Father

bd_q21_pai <-
  data_base_1 |> 
  filter(is.na(namorado_esposo_filtro)) |> 
  group_by(q21_pai) |> 
  summarise(N = n ()) |>
  ungroup() |> 
  rename (Description = q21_pai) |> 
  mutate(soma = sum(N),
         Percentage = N / soma * 100,
         #soma = NULL
  ) |> 
  filter(Description == 1) |> 
  mutate(Description = ifelse(Description == 1, "Father", 0))

##
# All data

bd <- rbind(
  bd_q21_mae,
  bd_q21_pai,
  bd_q21_filha,                
  bd_q21_filho,                 
  bd_q21_namorado_esposo,
  bd_q21_avomaterna,  
  bd_q21_avopaterna,            
  bd_q21_avomaterno,            
  bd_q21_avopaterno                  
) |> 
  mutate(
    Percentage = round(Percentage,2),
    
    Description = factor(Description,
                         levels = rev(c("Mother",
                                        "Father",
                                        "Daughter(s)",
                                        "Son(s)",
                                        "Spouse/Partner",
                                        "Maternal Grandmother",
                                        "Maternal Grandfather",
                                        "Paternal Grandmother",
                                        "Paternal Grandfather"
                                        
                         )))) |> 
  
  
  select (Description, N, Percentage) |> 
  # arrange(-Percentage) |> 
  gt() %>%
  tab_source_note(source_note = md("**Note:** The total values refer to the total N of participants, which is 2,501."))

#gtsave(bd, "tabela_cases.html")

rm(
  bd_q21_mae,
  bd_q21_pai,
  bd_q21_filha,                
  bd_q21_filho,                 
  bd_q21_namorado_esposo,
  bd_q21_avomaterna,  
  bd_q21_avopaterna,            
  bd_q21_avomaterno,            
  bd_q21_avopaterno,
  bd)

# . ----
###
#
## Appendix D  ----

# The total number of conflict cases: 107.

data_base_1 |> 
  filter(is.na(namorado_esposo_filtro)) |>
  summarise(Numero_casos = sum(q4_15_familiatoda))

# Distribution of conflicts by respondents: 88 respondents who declared conflicts

data_base_1 |>
  filter(is.na(namorado_esposo_filtro)) |>
  mutate(q4_15_familiatoda = as.character(q4_15_familiatoda)) |> 
  group_by(q4_15_familiatoda) |> 
  summarise(Numero_casos = n()) |> 
  rename(Número_conflitos = q4_15_familiatoda)

# This totals 88 unique cases.

banco <- 
  data_base_1 |> 
  filter(is.na(namorado_esposo_filtro)) |>
  
  
  select(q4_15_avomaterna,
         q4_15_avopaterna,
         q4_15_avomaterno,
         q4_15_avopaterno,
         q4_15_esposo,
         q4_15_filha,
         q4_15_filho,
         q4_15_mae,
         q4_15_namorado,
         q4_15_pai,
         q4_15_nao
         #q4_15_familiatoda
  ) |> 
  pivot_longer(cols = everything(), 
               names_to = "descricao", 
               values_to = "quantidade" ) |> 
  filter(descricao != "q4_15_nao") |> 
  mutate(descricao_modificada = case_when(
    descricao == "q4_15_avomaterna" ~ "Maternal Grandmother",
    descricao == "q4_15_avopaterna" ~ "Maternal Grandfather",
    descricao == "q4_15_avomaterno" ~ "Paternal Grandmother",
    descricao == "q4_15_avopaterno" ~ "Paternal Grandfather",
    descricao == "q4_15_esposo" ~ "Spouse/Partner",
    descricao == "q4_15_filha" ~ "Daughter(s)",
    descricao == "q4_15_filho" ~ "Son(s)",
    descricao == "q4_15_mae" ~ "Mother",
    descricao == "q4_15_namorado" ~ "Spouse/Partner",
    descricao == "q4_15_pai" ~ "Father",
    TRUE ~ descricao  
  )) |> 
  group_by(descricao_modificada) |> 
  summarise(quantitdade_total = sum(quantidade, na.rm = TRUE)) |> 
  ungroup() |> 
  mutate(soma = sum (quantitdade_total),
         perc = quantitdade_total/soma*100,
         perc = round(perc,2)) 

banco |> 
  ggplot()+
  aes(x = reorder(descricao_modificada, quantitdade_total) , y = quantitdade_total) +
  geom_col(width = 0.6, position = "dodge", alpha= .5)+
  geom_text(aes(
    label = quantitdade_total
  ), size = 3.2, position = position_dodge(width = 1), hjust = 0.04, vjust = 0)+
  #scale_y_continuous(labels = function(x) paste0(x, "%"), limits = c(0, 100))+
  #scale_x_reverse() +  
  coord_flip()+
  #theme_fivethirtyeight()+
  labs(
    title = NULL,
    subtitle = NULL, 
    x = NULL,
    y = "Number of conflicts",
    fill = NULL,
    caption = ""
  )+
  theme(legend.title = element_text(family = "Times New Roman"), 
        legend.position = "bottom",
        title = element_text(size = 11, family = "Times New Roman"),
        axis.title.y = element_text(size = 11, family = "Times New Roman"),
        axis.text.y = element_text(size = 11, family = "Times New Roman"),
        axis.text.x = element_text(size = 11, family = "Times New Roman"),
        axis.title.x = element_text(size = 11,family = "Times New Roman"),
        legend.text = element_text(size = 11, family = "Times New Roman"),
        plot.caption = element_text(hjust = 0, size = 11, family = "Times New Roman" ),
        panel.background = element_rect(fill = "white", colour = "white", color = "white"),
        plot.background = element_rect(fill = "white", colour = "white", color = "white"),
        legend.background = element_rect(fill = "white"),
        panel.grid.major.x = element_line(colour = "#eceff2", size = 0.7),
        panel.grid.major.y = element_line(colour = "#eceff2", size = 0.7),
        legend.key = element_rect(fill = "white", color = "white"),
        strip.background = element_rect(
          fill = "white", colour = "white"
        ))

# Title:Number of Family conflicts according to family member

# ggsave("artigos/familia_voto/resultados_familia_voto/apendex/conflit_por_membros.tiff",
#        width = 19, height = 14, units = "cm")

# Note: A total of 107 family conflicts were reported.

rm(banco)

# . ----
###
##
## Appendix E ----

# List models 
models_list <- list(m_1, m_2, m_3, m_4)

# Label models
dep_var_labels <- c("Model 1", "Model 2", "Model 3", "Model 4")

# Produce table with models
tab_model(models_list, show.rownames = FALSE, dv.labels = dep_var_labels)

rm(models_list,
   dep_var_labels,
   m_1, m_2, m_3, m_4)

# . ----
###
#
## Appendix F  ----

# Calculate the marginal effects of sexual orientation on the probability of family conflicts

sjPlot::plot_model(m_1, type = "pred",
                   #show.p = TRUE,
                   #mdrt.values = c("all"),
                   terms = c("Electoral divergences (ED)", "Non-heterosexual"), colors =c("#0088cc", "#999999")) +
  labs(title = "", 
       y = "Predicted probability of family conflicts (%)", 
       col = "Non-heterosexual",
       x = "Electoral divergences (ED)"
  )+
  scale_color_manual(name = "",
                     labels = c("Heterosexual", "Non-heterosexual"),
                     values = c("#1E90FF", "#994614"))+
  scale_fill_manual(values = c("#1E90FF", "#994614"))+
  scale_x_continuous(breaks = c(0, 1, 2, 3, 4, 5, 6, 7))+
  #scale_color_manual(labels = c("Homem", "Mulher"))#+
  #scale_fill_manual(values = c("#0088cc"))+
  #scale_y_continuous(limits = c(0, 0.6*1))+
  #scale_x_continuous(limits = c(0, 400))+
  theme(legend.title = element_text(family = "Times New Roman"),
        legend.position = "bottom",
        title = element_text(size = 11, family = "Times New Roman"),
        axis.title.y = element_text(size = 11, family = "Times New Roman"),
        axis.text.y = element_text(size = 11, family = "Times New Roman"),
        axis.text.x = element_text(size = 11, family = "Times New Roman"),
        axis.title.x = element_text(size = 11,family = "Times New Roman"),
        legend.text = element_text(size = 11, family = "Times New Roman"),
        plot.caption = element_text(hjust = 0, size = 11, family = "Times New Roman" ),
        panel.background = element_rect(fill = "white", colour = "white", color = "white"),
        plot.background = element_rect(fill = "white", colour = "white", color = "white"),
        legend.background = element_rect(fill = "white"),
        panel.grid.major.x = element_line(colour = "#eceff2", size = 0.7),
        panel.grid.major.y = element_line(colour = "#eceff2", size = 0.7),
        legend.key = element_rect(fill = "white", color = "white"),
        strip.background = element_rect(fill = "white", colour = "white"))


# ggsave("artigos/familia_voto/resultados_familia_voto/apendex/efeito_marginal_modelo_1.tiff",
#        width = 26, height = 12, units = "cm")

rm(m_1)

# . ----
###
##
# Appendix G ----

library(logistf)

# Standardize the independent variables

banco_padrao <- data_base_2 %>%
  mutate(
    `Electoral divergences (ED)` = scale(`Electoral divergences (ED)`),
    `Antagonism Lula supporters (ALS)` = scale(`Antagonism Lula supporters (ALS)`),
    `Antagonism Bolsonaro supporters (ABS)` = scale(`Antagonism Bolsonaro supporters (ABS)`),
    `Political salience` = scale(`Political salience`),
    `Women` = scale(`Women`),
    `Non-white` = scale(`Non-white`),
    `Non-heterosexual` = scale(`Non-heterosexual`),
    `Evangelical` = scale(`Evangelical`),
    Family_size = scale(Family_size),
    Age = scale(Age),
    Education = scale(Education),
    Income = scale(Income)
  )

## Model 1: Number of Conflicts - Beta standardized----

m_1_p <- logistf(
  q4_15_familiatoda  ~ 
    
    `Electoral divergences (ED)` +    
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income,
  firth = FALSE,
  pl = FALSE,
  data = banco_padrao)

##
## Model 2: Number of Conflicts - Beta standardized----
# Obs: INTERACTION: Electoral divergences*Antagonism - PT

m_2_p <- logistf(
  q4_15_familiatoda  ~ 
    
    `Electoral divergences (ED)` +     
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income +
    `Electoral divergences (ED)` * `Antagonism Lula supporters (ALS)`,
  firth = FALSE,
  pl = FALSE,
  data = banco_padrao
)

##
## Model 3: Number of Conflicts - Beta standardized----
# Obs: INTERACTION: Electoral divergences*Antagonism - Bolsonaro

m_3_p <- logistf(
  q4_15_familiatoda  ~ 
    
    `Electoral divergences (ED)` +     
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income+
    `Electoral divergences (ED)` * `Antagonism Bolsonaro supporters (ABS)` ,
  firth = FALSE,
  pl = FALSE,
  data = banco_padrao
)

##
## Model 4: Number of Conflicts - Beta standardized----
# Obs: INTERACTION: Electoral divergences*Non-heterosexual

m_4_p <- logistf(
  q4_15_familiatoda  ~ 
    
    `Electoral divergences (ED)` +   
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income+
    `Electoral divergences (ED)` * `Non-heterosexual`,
  firth = FALSE,
  pl = FALSE,
  data = banco_padrao
)

###
##
## All models - Beta standardized ----

models_list <- list(m_1_p, m_2_p, m_3_p, m_4_p)

dep_var_labels <- c("Model 1", "Model 2", "Model 3", "Model 4")

tab_model(models_list, show.rownames = FALSE, dv.labels = dep_var_labels, file = "tab_padronizada.html")

rm(m_1_p, m_2_p, m_3_p, m_4_p,
   models_list, dep_var_labels,
   banco_padrao)

# . ----
###
#
## Appendix H ----

# Main models, in OLS 

library(logistf)

### #Model 1: Number of Conflicts - OLS  ----

m_1 <- lm(
  q4_15_familiatoda  ~ 
    
    `Electoral divergences (ED)` +    
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income, 
  data = data_base_2)

##
### Model 2: Number of Conflicts - OLS  ----
# Obs: INTERACTION: Electoral divergences*Antagonism - PT

m_2 <- lm(
  q4_15_familiatoda  ~ 
    
    `Electoral divergences (ED)` +     
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income +
    `Electoral divergences (ED)` * `Antagonism Lula supporters (ALS)`,
  data = data_base_2
)

##
### Model 3: Number of Conflicts - OLS ----
# Obs: INTERACTION: Electoral divergences*Antagonism - Bolsonaro

m_3 <- lm(
  q4_15_familiatoda  ~ 
    
    `Electoral divergences (ED)` +     
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income+
    `Electoral divergences (ED)` * `Antagonism Bolsonaro supporters (ABS)`,
  data = data_base_2
)

##
### Model 4: Number of Conflicts - OLS ----
# Obs: INTERACTION: Electoral divergences*Non-heterosexual

m_4 <- lm(
  q4_15_familiatoda  ~ 
    
    `Electoral divergences (ED)` +   
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income+
    `Electoral divergences (ED)` * `Non-heterosexual`,
  data = data_base_2)

###
##
### All models - OLS ----

models_list <- list(m_1, m_2, m_3, m_4)

dep_var_labels <- c("Model 1", "Model 2", "Model 3", "Model 4")

tab_model(models_list, show.rownames = FALSE, dv.labels = dep_var_labels, file = "tab_ols.html")

rm(models_list, dep_var_labels)

# plot_models(m_4, m_3, m_2, m_1,
#             rm.terms = c("Age", "Education", "Income", "Family_size"), # Para não plotar no grafico
#             show.values = TRUE, # modelo grafico 
#             #value.offset = .3,
#             vline.color = "black",
#             line.size = 1,
#             m.labels = c("Model 4", "Model 3", "Model 2", "Model 1"),
#             ci.lvl = 0.95,
#             grid = TRUE,
#             show.legend = FALSE)+
#   scale_x_discrete(labels=
#                      list(
#                        `Electoral divergences (ED):Non-heterosexual` = "ED X Non-heterosexual",
#                        `Electoral divergences (ED):Antagonism Bolsonaro supporters (ABS)` = "ED X ABS",
#                        `Electoral divergences (ED):Antagonism Lula supporters (ALS)` = "ED X ALS"
#                      ))+
#   labs(
#     #title = "Family conflicts in the 2022 elections",
#     y = "Odds Ratios",
#     col = "")+
#   scale_y_continuous(limits = c(0, 4))+
#   scale_color_manual(values = c("#333133", "#B1B1B1","#333133","#B1B1B1"))+
#   theme_fivethirtyeight()+
#   theme(legend.title = element_text(family = "Times New Roman"), 
#         legend.position = "bottom",
#         title = element_text(size = 11, family = "Times New Roman"),
#         axis.title.y = element_text(size = 11, family = "Times New Roman"),
#         axis.text.y = element_text(size = 11, family = "Times New Roman"),
#         axis.text.x = element_text(size = 11, family = "Times New Roman"),
#         axis.title.x = element_text(size = 11,family = "Times New Roman"),
#         legend.text = element_text(size = 11, family = "Times New Roman"),
#         plot.caption = element_text(hjust = 0, size = 11, family = "Times New Roman" ),
#         panel.background = element_rect(fill = "white", colour = "white", color = "white"),
#         plot.background = element_rect(fill = "white", colour = "white", color = "white"),
#         legend.background = element_rect(fill = "white"),
#         panel.grid.major.x = element_line(colour = "#eceff2", size = 0.7),
#         panel.grid.major.y = element_line(colour = "#eceff2", size = 0.7),
#         legend.key = element_rect(fill = "white", color = "white"),
#         strip.background = element_rect(fill = "white", colour = "white"))

#ggsave("artigos/familia_voto/resultados_familia_voto/m_dummy_grid.tiff", width = 26, height = 12, units = "cm")

rm(m_4, m_3, m_2, m_1)

# . ----
###
#
## Appendix I ----

# . ----
###
#
## Appendix I. 1 - Rare event ----

# Main models with alternative operationalization of non-heterosexual

library(logistf)

#table(modelos_banco_pos_eleicao_wide$q3_8) #  75 cases

m_ns <- data_base_2 |> 
  filter(!q3_8 %in% c("Prefiro n√£o responder", "N√£o sei"))

#rm(modelos_banco_pos_eleicao_wide)

### Model 1: Number of Conflicts - Alternative operationalization of non-heterosexual (rare event) ----

m_1_c <- logistf(
  q4_15_familiatoda  ~ 
    
    `Electoral divergences (ED)` +    
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income,
  firth = FALSE,
  pl = FALSE,
  data = m_ns)

##
### Model 2: Number of Conflicts - Alternative operationalization of non-heterosexual (rare event) ----
# Obs: INTERACTION: Electoral divergences*Antagonism - PT

m_2_c <- logistf(
  q4_15_familiatoda  ~ 
    
    `Electoral divergences (ED)` +     
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income +
    `Electoral divergences (ED)` * `Antagonism Lula supporters (ALS)`,
  firth = FALSE,
  pl = FALSE,
  data = m_ns
)

##
### Model 3: Number of Conflicts - Alternative operationalization of non-heterosexual (rare event) ----
# Obs: INTERACTION: Electoral divergences*Antagonism - Bolsonaro

m_3_c <- logistf(
  q4_15_familiatoda  ~ 
    
    `Electoral divergences (ED)` +     
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income+
    `Electoral divergences (ED)` * `Antagonism Bolsonaro supporters (ABS)` ,
  firth = FALSE,
  pl = FALSE,
  data = m_ns
)

##
### Model 4: Number of Conflicts - Alternative operationalization of non-heterosexual (rare event) ----
# Obs: INTERACTION: Electoral divergences*Non-heterosexual

m_4_c <- logistf(
  q4_15_familiatoda  ~ 
    
    `Electoral divergences (ED)` +   
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income+
    `Electoral divergences (ED)` * `Non-heterosexual`,
  firth = FALSE,
  pl = FALSE,
  data = m_ns
)

###
##
### All models - Alternative operationalization of non-heterosexual (rare event) ----

models_list <- list(m_1_c, m_2_c, m_3_c, m_4_c)

dep_var_labels <- c("Model 1", "Model 2", "Model 3", "Model 4")

tab_model(models_list, show.rownames = FALSE, dv.labels = dep_var_labels, file = "tab_sem_naosei.html")

rm(m_1_c, m_2_c, m_3_c, m_4_c, m_ns)

rm(models_list, dep_var_labels)

## OBS:
# The below is the number of cases that are excluded from the analyses due to alternative operationalization:
# i.e., the below is the number of cases missing information on sexuality. 
# (58+17) 

# . ----
###
##
# Appendix I. 2 ----

# Rare event beta standardized models

# Standardize the independent variables

banco_padrao_m_ns <- data_base_2 %>%
  mutate(
    `Electoral divergences (ED)` = scale(`Electoral divergences (ED)`),
    `Antagonism Lula supporters (ALS)` = scale(`Antagonism Lula supporters (ALS)`),
    `Antagonism Bolsonaro supporters (ABS)` = scale(`Antagonism Bolsonaro supporters (ABS)`),
    `Political salience` = scale(`Political salience`),
    `Women` = scale(`Women`),
    `Non-white` = scale(`Non-white`),
    `Non-heterosexual` = scale(`Non-heterosexual`),
    `Evangelical` = scale(`Evangelical`),
    Family_size = scale(Family_size),
    Age = scale(Age),
    Education = scale(Education),
    Income = scale(Income)
  ) |> 
  filter(!q3_8 %in% c("Prefiro n√£o responder", "N√£o sei"))

## Model 1: Number of Conflicts - Alternative operationalization of non-heterosexual (beta standardized) ----

m_1_p_m_ns <- logistf(
  q4_15_familiatoda  ~ 
    
    `Electoral divergences (ED)` +    
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income,
  firth = FALSE,
  pl = FALSE,
  data = banco_padrao_m_ns)

##
## Model 2: Number of Conflicts - Alternative operationalization of non-heterosexual (beta standardized)  ----
# Obs: INTERACTION: Electoral divergences*Antagonism - PT

m_2_p_m_ns <- logistf(
  q4_15_familiatoda  ~ 
    
    `Electoral divergences (ED)` +     
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income +
    `Electoral divergences (ED)` * `Antagonism Lula supporters (ALS)`,
  firth = FALSE,
  pl = FALSE,
  data = banco_padrao_m_ns
)

##
## Model 3: Number of Conflicts - Alternative operationalization of non-heterosexual (beta standardized) ----
# Obs: INTERACTION: Electoral divergences*Antagonism - Bolsonaro

m_3_p_m_ns <- logistf(
  q4_15_familiatoda  ~ 
    
    `Electoral divergences (ED)` +     
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income+
    `Electoral divergences (ED)` * `Antagonism Bolsonaro supporters (ABS)` ,
  firth = FALSE,
  pl = FALSE,
  data = banco_padrao_m_ns
)

##
## Model 4: Number of Conflicts - Alternative operationalization of non-heterosexual (beta standardized) ----
# Obs: INTERACTION: Electoral divergences*Non-heterosexual

m_4_p_m_ns <- logistf(
  q4_15_familiatoda  ~ 
    
    `Electoral divergences (ED)` +   
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income+
    `Electoral divergences (ED)` * `Non-heterosexual`,
  firth = FALSE,
  pl = FALSE,
  data = banco_padrao_m_ns
)

###
##
## All models - Alternative operationalization of non-heterosexual (beta standardized) ----

models_list <- list(m_1_p_m_ns, m_2_p_m_ns,
                    m_3_p_m_ns, m_4_p_m_ns)

dep_var_labels <- c("Model 1", "Model 2", "Model 3", "Model 4")

tab_model(models_list, show.rownames = FALSE, dv.labels = dep_var_labels, file = "tab_padronizada_m_ns.html")

rm(m_1_p_m_ns, m_2_p_m_ns, m_3_p_m_ns, m_4_p_m_ns,
   models_list, dep_var_labels,
   banco_padrao_m_ns)

# . ----
###
#
##  Appendix I. 3 ----

# OLS

library(logistf)

m_ns_ols <- data_base_2 |> 
  filter(!q3_8 %in% c("Prefiro n√£o responder", "N√£o sei"))

### #Model 1: Number of Conflicts - Alternative operationalization of non-heterosexual (OLS) ----

m_1_m_ns_ols <- lm(
  q4_15_familiatoda  ~ 
    
    `Electoral divergences (ED)` +    
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income, 
  data = m_ns_ols)


##
### Model 2: Number of Conflicts - Alternative operationalization of non-heterosexual (OLS) ----
# Obs: INTERACTION: Electoral divergences*Antagonism - PT

m_2_m_ns_ols <- lm(
  q4_15_familiatoda  ~ # variavel dependente
    
    `Electoral divergences (ED)` +     
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income +
    `Electoral divergences (ED)` * `Antagonism Lula supporters (ALS)`,
  data = m_ns_ols
)

##
### Model 3: Number of Conflicts - Alternative operationalization of non-heterosexual (OLS) ----
# Obs: INTERACTION: Electoral divergences*Antagonism - Bolsonaro

m_3_m_ns_ols <- lm(
  q4_15_familiatoda  ~
    
    `Electoral divergences (ED)` +     
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income+
    `Electoral divergences (ED)` * `Antagonism Bolsonaro supporters (ABS)`,
  data = m_ns_ols
)

##
### Model 4: Number of Conflicts - Alternative operationalization of non-heterosexual (OLS) ----
# Obs: INTERACTION: Electoral divergences*Non-heterosexual

m_4_m_ns_ols <- lm(
  q4_15_familiatoda  ~ 
    
    `Electoral divergences (ED)` +   
    `Antagonism Lula supporters (ALS)` +
    `Antagonism Bolsonaro supporters (ABS)` +
    `Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    Family_size +
    Age + 
    Education +  
    Income+
    `Electoral divergences (ED)` * `Non-heterosexual`,
  data = m_ns_ols)

###
##
### All models - Alternative operationalization of non-heterosexual (OLS) ----

models_list <- list(m_1_m_ns_ols, m_2_m_ns_ols,
                    m_3_m_ns_ols, m_4_m_ns_ols)

dep_var_labels <- c("Model 1", "Model 2", "Model 3", "Model 4")

tab_model(models_list, show.rownames = FALSE, dv.labels = dep_var_labels, file = "tab_ols_m_ns.html")

rm(m_1_m_ns_ols, m_2_m_ns_ols,
   m_3_m_ns_ols, m_4_m_ns_ols,
   dep_var_labels,
   models_list,
   m_ns_ols)

# . ----
###
#
## Appendix J ----

# Analyses of potential mechanisms

### # Model, DV: Political salience  ----

# a <- modelos_banco_pos_eleicao_wide |>
#   select(`Political salience`)

m_1 <- lm(
  `Political salience` ~ 
    #`Electoral divergences (ED)` +    
    #`Antagonism Lula supporters (ALS)` +
    #`Antagonism Bolsonaro supporters (ABS)` +
    #`Political salience` + # E a variavel 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    #Family_size +
    Age + 
    Education +  
    Income, 
  data = data_base_2)

#tab_model(m_1) # Tab

### Model, DV: Electoral divergences ----

m_2 <- lm(
  `Electoral divergences (ED)` ~ 
    
    #`Electoral divergences (ED)` +    
    #`Antagonism Lula supporters (ALS)` +
    #`Antagonism Bolsonaro supporters (ABS)` +
    #`Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    #Family_size +
    Age + 
    Education +  
    Income, 
  data = data_base_2)

#tab_model(m_2) # Tab

### Model, DV: Antagonism Bolsonaro supporters (ABS) ----

m_3 <- lm(
  `Antagonism Bolsonaro supporters (ABS)` ~ # variavel dependente
    
    #`Electoral divergences (ED)` +    
    #`Antagonism Lula supporters (ALS)` +
    #`Antagonism Bolsonaro supporters (ABS)` +
    #`Political salience` + 
    `Women` + 
    `Non-white` + 
    `Non-heterosexual` +  
    `Evangelical` + 
    
    #Family_size +
    Age + 
    Education +  
    Income, 
  data = data_base_2)

#tab_model(m_3) # Tab

###
##
### All models exploring mechanisms ----

models_list <- list(m_2, m_1, m_3)

dep_var_labels <- c("Electoral divergences", "Political salience",
                    "Antagonism Bolsonaro supporters (ABS)")

tab_model(models_list, show.rownames = FALSE, dv.labels = dep_var_labels, file = "tab_politica.html")

rm(dep_var_labels, models_list)


###
##
### Produce figure ----

plot_models(m_3, m_1, m_2,
            #rm.terms = c(), 
            show.values = TRUE, 
            #value.offset = .3,
            vline.color = "black",
            line.size = 1,
            m.labels = c("Antagonism Bolsonaro supporters (ABS)",
                         "Political salience",
                         "Electoral divergences"),
            ci.lvl = 0.95,
            grid = TRUE,
            show.legend = FALSE)+
  labs(
    title = NULL,
    y = "Estimates",
    col = "")+
  #scale_y_continuous(limits = c(-0.25, 0.25))+
  scale_color_manual(values = c("#333133", "#B1B1B1","#333133","#B1B1B1"))+
  theme_fivethirtyeight()+
  theme(legend.title = element_text(family = "Times New Roman"), 
        legend.position = "bottom",
        title = element_text(size = 11, family = "Times New Roman"),
        axis.title.y = element_text(size = 11, family = "Times New Roman"),
        axis.text.y = element_text(size = 11, family = "Times New Roman"),
        axis.text.x = element_text(size = 11, family = "Times New Roman"),
        axis.title.x = element_text(size = 11,family = "Times New Roman"),
        legend.text = element_text(size = 11, family = "Times New Roman"),
        plot.caption = element_text(hjust = 0, size = 11, family = "Times New Roman" ),
        panel.background = element_rect(fill = "white", colour = "white", color = "white"),
        plot.background = element_rect(fill = "white", colour = "white", color = "white"),
        legend.background = element_rect(fill = "white"),
        panel.grid.major.x = element_line(colour = "#eceff2", size = 0.7),
        panel.grid.major.y = element_line(colour = "#eceff2", size = 0.7),
        legend.key = element_rect(fill = "white", color = "white"),
        strip.background = element_rect(fill = "white", colour = "white"))

# ggsave("artigos/familia_voto/resultados_familia_voto/apendex/tab_politica.tiff",
#        width = 26, height = 12, units = "cm")

rm(
  models_list,
  dep_var_labels,
  m_1,
  m_2,
  m_3)
